package pre.lxm.demo.rollview;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class SizeChangeRelativeLayout extends RelativeLayout {

    private int dividerMargin = 20;
    private int mLineCount = 3;
    private List<Square> squares;
    private String[] mColors = new String[]{"#FF0000", "#FF00F0", "#0000FF",
            "#FFF000", "#00FF00", "#00FFFF",
            "#FF00FF", "#FF0F00", "#FFFF00"};

    //    private String[] mColors = new String[]{"#FF0000", "#FF0000", "#FF0000",
//            "#FF0000", "#FF0000", "#FF0000",
//            "#FF0000", "#FF0000", "#FF0000"};
    public String TAG = "SizeChangeLayout";
    int itemWidth;

    public SizeChangeRelativeLayout(Context context) {
        this(context, null);
    }

    public SizeChangeRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SizeChangeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        squares = new ArrayList<>();
//        for (int i = 0; i < 9; i++) {
//            Square mSquare = new Square();
//            mSquare.mTv = new TextView(getContext());
//            mSquare.mStr = String.valueOf(i);
//            mSquare.mBackColor = Color.parseColor(mColors[i]);
//            squares.add(mSquare);
//
//            mSquare.mTv.setBackgroundColor(mSquare.mBackColor);
//            mSquare.mTv.setTextColor(Color.WHITE);
//            mSquare.mTv.setGravity(Gravity.CENTER);
//            mSquare.mTv.setTextSize(20);
//            mSquare.mTv.setId(i + 1);
//            mSquare.mTv.setText(mSquare.mStr);
////            addView(mSquare.mTv);
//        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
//        Paint mPaint = new Paint();
//        mPaint.setColor(Color.WHITE);
//        mPaint.setStyle(Paint.Style.FILL);
//        mPaint.setStrokeWidth(10);
//        canvas.save();
//        int squareWidth = getWidth() / mLineCount;
//        for (int i = 0; i < squares.size(); i++) {
//            if ((i + 1) % 3 != 1) {
//                canvas.translate(squareWidth, 0);
//            }
//            if (i / 3 > 0) {
//                canvas.translate(0, squareWidth);
//            }
//            canvas.drawLine(0, 0, squareWidth, squareWidth, mPaint);
//        }
//        canvas.restore();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.e(TAG, "--onLayout--\twidth:" + getWidth() + ",\theight:" + getHeight());
        itemWidth = (getWidth() - dividerMargin * (mLineCount + 1)) / mLineCount;
//        int itemHeiht = (getHeight() - dividerMargin * (mLineCount + 1)) / mLineCount;
        for (int i = 0; i < 8; i++) {
            Square mSquare = new Square();
            mSquare.mTv = new TextView(getContext());
            mSquare.mStr = String.valueOf(i);
            mSquare.mBackColor = Color.parseColor(mColors[i]);
            squares.add(mSquare);

            mSquare.mTv.setBackgroundColor(mSquare.mBackColor);
            mSquare.mTv.setTextColor(Color.WHITE);
            mSquare.mTv.setGravity(Gravity.CENTER);
            mSquare.mTv.setTextSize(20);
            mSquare.mTv.setId(i + 1);
            mSquare.mTv.setText(mSquare.mStr);
//            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
//            if ((i + 1) % mLineCount == 1) {
//                rlp.leftMargin = dividerMargin;
//                rlp.topMargin = dividerMargin;
//                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
//            } else {
//                rlp.leftMargin = dividerMargin;
//                rlp.addRule(RelativeLayout.ALIGN_TOP, squares.get(i - 1).mTv.getId());
//                rlp.addRule(RelativeLayout.RIGHT_OF, squares.get(i - 1).mTv.getId());
//            }
//            if (i / 3 > 0) {
//                rlp.addRule(RelativeLayout.BELOW, squares.get(i - 3).mTv.getId());
//            }

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams
                    .WRAP_CONTENT);
            if (i == 0) {
                rlp.width = itemWidth * 2 + dividerMargin;
                rlp.height = itemWidth * 2 + dividerMargin;
                rlp.leftMargin = dividerMargin;
                rlp.topMargin = dividerMargin;
                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            } else if (i == 7) {
                rlp.width = itemWidth;
                rlp.height = itemWidth;
                rlp.rightMargin = dividerMargin;
                rlp.bottomMargin = dividerMargin;
                rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            } else {
                if (i >= 1 && i <= 3) {
                    rlp.width = itemWidth;
                    rlp.height = (itemWidth * 2 - dividerMargin) / 3;
                    rlp.addRule(RelativeLayout.RIGHT_OF, squares.get(0).mTv.getId());
                    rlp.leftMargin = dividerMargin;
                    if (i == 1) {
                        rlp.addRule(RelativeLayout.ALIGN_TOP, squares.get(0).mTv.getId());
                    } else {
                        rlp.topMargin = dividerMargin;
                        rlp.addRule(RelativeLayout.BELOW, squares.get(i - 1).mTv.getId());
                    }
                } else if (i >= 4 && i <= 6) {
                    rlp.width = (itemWidth * 2 - dividerMargin) / 3;
                    rlp.height = itemWidth;
                    rlp.addRule(RelativeLayout.BELOW, squares.get(0).mTv.getId());
                    rlp.topMargin = dividerMargin;
                    if (i == 4) {
                        rlp.addRule(RelativeLayout.ALIGN_LEFT, squares.get(0).mTv.getId());
                    } else {
                        rlp.leftMargin = dividerMargin;
                        rlp.addRule(RelativeLayout.RIGHT_OF, squares.get(i - 1).mTv.getId());
                    }
                }
            }
            addView(mSquare.mTv, i, rlp);
            mSquare.mTv.setLayoutParams(rlp);
        }
        Log.e(TAG, "childCount:" + getChildCount());

    }


    //i : 0 -8
    public void change() {
        for (int i = 0; i < getChildCount(); i++) {
            TextView mTv = squares.get(i).mTv;



        }
    }

    class Square {
        TextView mTv;
        String mStr;
        int mBackColor;
    }

}
